package com.notice.rule.print;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import com.notice.entity.NoticePrint;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 堆栈加工
 *
 * @author DZL
 * @date 2023/1/12 16:35
 */
@Component
public class StackMsgProcess extends AbstractNoticeMsgProcessHandler {
    @Override
    public int order() {
        return -2;
    }

    @Override
    public NoticePrint printProcessing(ILoggingEvent event, NoticePrint noticePrint) {
        StringBuilder sb = new StringBuilder("堆栈信息：\n\n");
        IThrowableProxy throwableProxy = event.getThrowableProxy();
        if (throwableProxy != null) {
            sb.append(throwableProxy.getClassName()).append(":").append(throwableProxy.getMessage()).append("\n");
            //详细错误信息
            Arrays.stream(throwableProxy.getStackTraceElementProxyArray()).forEach(x -> {
                sb.append(x.getSTEAsString()).append("\n");
            });
        }
        noticePrint.setStackMsg(sb.toString());
        return noticePrint;
    }
}
